package code.framework.android.core;

import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.view.LayoutInflater;

public class ApplicationContextHelper {

	private static final int BUFFER_SIZE = 32 * 1024;

	public static byte[] getAssetAsBytes(String asset, Context context)
			throws IOException {
		InputStream inputStream = null;
		try {
			inputStream = context.getAssets().open(asset);
			int size = inputStream.available();
			// System.out.println("size = "+size);
			if (size == 0) {
				size = BUFFER_SIZE;
			}
			byte[] buffer = new byte[size];
			byte[] result = null;
			int read = 0;
			while ((read = inputStream.read(buffer)) > 0) {
				// System.out.println("read = "+read);
				byte[] tempBytes = new byte[read];
				System.arraycopy(buffer, 0, tempBytes, 0, read);
				if (result == null) {
					result = new byte[read];
					// System.out.println("result.length = "+result.length);
					System.arraycopy(tempBytes, 0, result, 0, read);
				} else {
					// swap original bytes first
					int orignal = result.length;
					byte[] swapBytes = new byte[orignal];
					System.arraycopy(result, 0, swapBytes, 0, orignal);
					result = new byte[result.length + read];
					System.arraycopy(swapBytes, 0, result, 0, orignal);
					// copy new byte now
					System.arraycopy(tempBytes, orignal, result, orignal, read);
				}
			}
			return result;
		} finally {
			inputStream.close();
		}
	}

	public static byte[] getAssetAsBytes1(String asset, Context context)
			throws IOException {
		final InputStream inputStream = context.getAssets().openFd(asset)
				.createInputStream();
		int size = inputStream.available();
		// System.out.println("size = "+size);
		if (size == 0) {
			size = BUFFER_SIZE;
		}
		byte[] buffer = new byte[size];
		byte[] result = null;
		int read = 0;
		while ((read = inputStream.read(buffer)) > 0) {
			// System.out.println("read = "+read);
			byte[] tempBytes = new byte[read];
			System.arraycopy(buffer, 0, tempBytes, 0, read);
			if (result == null) {
				result = new byte[read];
				// System.out.println("result.length = "+result.length);
				System.arraycopy(tempBytes, 0, result, 0, read);
			} else {
				// swap original bytes first
				int orignal = result.length;
				byte[] swapBytes = new byte[orignal];
				System.arraycopy(result, 0, swapBytes, 0, orignal);
				result = new byte[result.length + read];
				System.arraycopy(swapBytes, 0, result, 0, orignal);
				// copy new byte now
				System.arraycopy(tempBytes, orignal, result, orignal, read);
			}
		}
		return result;
	}

	public static String convertClassNameToDbName(String className) {
    	StringBuilder result = new StringBuilder(className.length());
    	for (int i=0;i<className.length();i++) {
    		char c = className.charAt(i);
    		if (i == 0) {
    			result.append(String.valueOf(c).toLowerCase());
    		} else {
    			if (c >= 'A' && c <= 'Z') {
        			result.append("_"+String.valueOf(c).toLowerCase());
        		} else {
        			result.append(String.valueOf(c));
        		}
    		}
    	}
    	return result.toString();
	}

	public static LayoutInflater getLayoutInflater(Context context) {
		// return (LayoutInflater) context
		// .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		return LayoutInflater.from(context);
	}

	private static DateFormat datetimeFormat = new SimpleDateFormat(
			"yyyyMMddHHmmss");

	// private AndroidFormatUtils() {
	// datetimeFormat
	// }

	public static String format(Date date) {
		return datetimeFormat.format(date);
	}

}
